// 播放列表弹窗
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:music_app/controllers/audio_player_controller.dart';
import 'package:music_app/widget/list_song_item.dart';

class PlayListDialog extends StatelessWidget {
  const PlayListDialog({
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    final audioController = Get.find<AudioPlayerController>();
    final list = audioController.playlist;
    return SafeArea(
      child: Container(
        padding: const EdgeInsets.all(16),
        decoration: const BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.vertical(top: Radius.circular(16)),
        ),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            const Text(
              '当前播放',
              style: TextStyle(
                fontSize: 18,
                fontWeight: FontWeight.bold,
                color: Colors.black,
              ),
            ),
            const SizedBox(height: 8),
            SizedBox(
              height: 400,
              child: ListView.builder(
                // 作用：移除 ListView 的默认内边距，让内容贴紧边界。
                padding: EdgeInsets.zero,
                itemCount: list.length,
                itemBuilder: (context, index) {
                  return _buildSongItem(list[index]);
                },
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildSongItem(Map<String, dynamic> song) {
    final audioController = Get.find<AudioPlayerController>();
    return ListSongItem(
      id: song['id'],
      fee: song['fee'],
      imageUrl: song['al']['picUrl'],
      title: song['name'],
      artist: (song['ar'] as List)
          .map(
            (artist) => artist['name'],
          )
          .join(', '),
      onTap: () {
        audioController.playSong(song);
      },
    );
  }
}
